//
//  CityLocationViewController.m
//  OCUI
//
//  Created by login on 15-7-17.
//
//

#import "CityLocationViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface CityLocationViewController ()<UIAlertViewDelegate,CLLocationManagerDelegate>
{
    CLLocationManager *myLocationManager;
    UILabel *cityLabel;
}
@end

@implementation CityLocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (![CLLocationManager locationServicesEnabled]) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"消息提示" message:@"请到设置中心去设置开启定位服务" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
        alert.tag=ServicesEnabled;
        [alert show];
    }
    [self initLocationManager];
    
    cityLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100, 50)];
    cityLabel.text = @"sad";
    cityLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:cityLabel];
    

}
-(void)initLocationManager
{
    if (myLocationManager) {
        myLocationManager=nil;
        myLocationManager.delegate=nil;
    }
    myLocationManager=[[CLLocationManager alloc]init];
    myLocationManager.desiredAccuracy=kCLLocationAccuracyBest;
    myLocationManager.delegate=self;
    if ([[[UIDevice currentDevice] systemVersion]floatValue]>=8.0) {
        [myLocationManager requestAlwaysAuthorization];
    }
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //检查弹出框的点击按钮是哪一个
    NSLog(@"alertView tag %ld buttonIndex %ld",(long)alertView.tag,(long)buttonIndex);
    AlertTag alertTag=(int)alertView.tag;
    switch (alertTag) {
        case ServicesEnabled: {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            break;
        }
        case RequestLocation: {
            if (buttonIndex!=0) {
                [self initLocationManager];
            }
            break;
        }
        default: {
            break;
        }
    }
    
    
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined: {
            NSLog(@"用户未决定开启定位服务");
            break;
        }
        case kCLAuthorizationStatusDenied: {
            {
//                UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示信息" message:@"如果拒绝将无法使用本软件" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"ok", nil];
//                alertView.tag=RequestLocation;
//                [alertView show];
            }
            break;
        }
        case kCLAuthorizationStatusAuthorizedAlways: {
            [myLocationManager startUpdatingLocation];
            break;
        }
            
        case kCLAuthorizationStatusAuthorizedWhenInUse: {
            
            break;
        }
        default: {
            break;
        }
    }
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
    [myLocationManager stopUpdatingLocation];
    NSLog(@"location ok");
    
    NSLog(@"%@",[NSString stringWithFormat:@"经度:%3.5f\n纬度:%3.5f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]);
    
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        for (CLPlacemark * placemark in placemarks) {
            
            NSDictionary *test = [placemark addressDictionary];
            //  Country(国家)  State(城市)  SubLocality(区)
            NSLog(@"%@", [test objectForKey:@"State"]);
            
            [self performSelectorOnMainThread:@selector(updateMain:) withObject:test waitUntilDone:YES];
        }
    }];
    
}

- (void)updateMain:(NSDictionary *)dic
{
    cityLabel.text = dic[@"State"];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
